<?php
/**
 * Created by PhpStorm.
 * User: zhangjunliang
 * Date: 18/12/15
 * Time: 下午2:25
 */

namespace App\Libraries;


class Error
{
    //通用号段区
    const ERR_OK                  = '9999';
    const ERR_ERROR               = '9998';
    const ERR_SYSTEM_ERROR        = '9000';
    const ERR_LOGININFO_ERROR     = '9001';
    const ERR_AUTHINFO_ERROR      = '9002';
    const ERR_NO_LOGIN            = '9003';
    const ERR_UID                 = '9004';
    const ERR_MYSQL_GET_LOCK      = '9005';
    const ERR_BUSY                = '9006';
    const ERR_PARAM_EMPTY         = '9007';
    const ERR_PARAM_TYPE          = '9008';
    const ERR_PARAM_MONEY         = '9009';
    const ERR_PARAM_UID           = '9010';
    const ERR_PARAM_INVALID       = '9011';
    const ERR_INSERT_FAIL         = '9012';
    const ERR_UPDATE_FAIL         = '9013';
    const ERR_CAPTCHA_ERROR       = '9014';
    const ERR_IMAGE_CAPTCHA_ERROR = '9015';
    const ERR_NOT_EXISTX          = '9016';
    const ERR_MOBILE_INVALID      = '9017';
    const ERR_NOT_MODIFICATION    = '9018';
    const ERR_REGISTER_FAILED     = '9019';
    const ERR_EXISTS              = '9020';
    const ERR_CURL                = '9021';
    const ERR_GUEST_ERROR         = '9022';
    const ERR_TOKEN_ERROR         = '8999';
    const ERR_SIGN_ERROR          = '1001';
    const ERR_BIND_ERROR          = '1002';
    const ERR_STEP_ERROR          = '1003';
    const ERR_BINDINFO_ERROR      = '1004';
    const ERR_GOLD_NOT_ENOUGH     = '1005';
    const ERR_NO_EMPLOYEE         = '1006';

    private static $_msg = [
        self::ERR_OK                                 => '操作成功',
        self::ERR_ERROR                              => '操作失败',
        self::ERR_SYSTEM_ERROR                       => '系统错误',
        self::ERR_LOGININFO_ERROR                    => '登录信息错误',
        self::ERR_AUTHINFO_ERROR                     => '您没有权限查看该页',
        self::ERR_NO_LOGIN                           => '账号未登录,请登录',
        self::ERR_UID                                => '请刷新页面，检查当前账号',
        self::ERR_MYSQL_GET_LOCK                     => '数据库锁失败，请重试',
        self::ERR_BUSY                               => '系统繁忙，请稍后再试！',
        self::ERR_PARAM_EMPTY                        => '参数为空',
        self::ERR_PARAM_TYPE                         => '参数类型错误',
        self::ERR_PARAM_MONEY                        => '金额错误',
        self::ERR_PARAM_UID                          => 'uid错误',
        self::ERR_PARAM_INVALID                      => '参数不合法',
        self::ERR_INSERT_FAIL                        => '创建失败',
        self::ERR_UPDATE_FAIL                        => '更新失败',
        self::ERR_CAPTCHA_ERROR                      => '验证码错误',
        self::ERR_IMAGE_CAPTCHA_ERROR                => '图形验证码错误',
        self::ERR_NOT_EXISTX                         => '记录不存在',
        self::ERR_MOBILE_INVALID                     => '手机号码格式错误',
        self::ERR_NOT_MODIFICATION                   => '没有做任何修改',
        self::ERR_REGISTER_FAILED                    => '注册失败',
        self::ERR_EXISTS                             => '已存在',
        self::ERR_GUEST_ERROR                        => '游客模式登录错误',
        self::ERR_TOKEN_ERROR                        => 'token失效',
        self::ERR_SIGN_ERROR                         => '今天已经签过到了',
        self::ERR_BIND_ERROR                         => '已经绑定过手机号了',
        self::ERR_STEP_ERROR                         => '今天步数已经兑换过了',
        self::ERR_BINDINFO_ERROR                     => '已经绑定过信息了',
        self::ERR_GOLD_NOT_ENOUGH                    => '金币数量不足，不可以兑换',
        self::ERR_NO_EMPLOYEE                        => '员工身份验证未通过',
    ];
    
    private static function nullToEmpty(&$data)
    {
        is_array($data) ? array_walk_recursive($data, function(&$input) {
            $input = is_null($input) ? '' : $input;
        }) : self::nullToEmpty($data);
    }
    
    private static function getMsg($e)
    {
        if (empty($e))
        {
            return '';
        }
        if (!array_key_exists($e, self::$_msg))
        {
            return '未知错误';
        }
        return self::$_msg[$e];
    }
    
    /**
     * format 格式化结果信息
     * @param  string $errno 错误号
     * @param  string $data  数据
     * @param  string $m     自定义提示信息
     * @return array
     */
    public static function format($errno, $data = null, $m = '')
    {
        $m = $m ? $m : self::getMsg($errno);
        $result = array(
            'e' => $errno,
            'm' => $m
        );
        if (!is_null($data) && !empty($data))
        {
            $result['data'] = $data;
        }
        self::nullToEmpty($result);
        return $result;
    }
}